(function(){

'use strict';

angular.module('controllers.faultLocation.pbossOm')
	.controller('orderRelationController', orderRelationController)
	.controller('industryCtrl', industryCtrl);

function orderRelationController($scope, pbossOmServer, $filter, CoreService, COREDATA, $mdDialog,CoreLoading,productDataStores) {
	$scope.flag = false;
	$scope.shoumun = 1;
	$scope.errorInfo = COREDATA.ERRORINFO; //日期错误信息
	$scope.provinces = COREDATA.PROVINCES;
	productDataStores.setCodeInit();
	$scope.translate = {
		provinceCodes: [],
		industryCode: [],
		// industryName: productDataStores.data.productName.join()||'全部',
		typeCode: [],
		userTypeCode: [],
		orderChannelCode: [],
		startTime: new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate() - 31),
		endTime: new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate() - 1)
	};

	$scope.industryName = productDataStores.data.productName;

	$scope.$watch('industryName', function(newValue,oldValue){
		$scope.a = newValue.join() || '全部';
	},true);
	$scope.industryList = [{
		code: '01',
		interpretation: '公共事业'
	}, {
		code: '02',
		interpretation: '物流管理'
	}, {
		code: '03',
		interpretation: '建筑和家居'
	}, {
		code: '04',
		interpretation: '交通和运输'
	}, {
		code: '05',
		interpretation: '工业制造'
	}, {
		code: '06',
		interpretation: '医疗卫生'
	}, {
		code: '07',
		interpretation: '金融服务'
	}, {
		code: '08',
		interpretation: '农业'
	}, {
		code: '09',
		interpretation: '水利'
	}];
	$scope.channelList = [{
		code: '01',
		interpretation: 'WEP'
	}, {
		code: '02',
		interpretation: '营业厅'
	}, {
		code: '03',
		interpretation: 'SMS'
	}, {
		code: '04',
		interpretation: '语音IVP'
	}, {
		code: '05',
		interpretation: 'MMS'
	}, {
		code: '06',
		interpretation: 'BOSS后台批量'
	}, {
		code: '07',
		interpretation: 'SP'
	}];
	$scope.cardList = [{
		code: '01',
		interpretation: 'Sim卡'
	}, {
		code: '02',
		interpretation: 'MP卡'
	}, {
		code: '03',
		interpretation: 'MS卡'
	}];
	$scope.userList = [{
		code: '01',
		interpretation: '个人用户'
	}, {
		code: '02',
		interpretation: '集团用户'
	}];
	$scope.queryOrderRelation = function() {
		var paramsData = {};
		var provinceCodes;
		$scope.shoumun == 1 ? $scope.shoumun = 0 : $scope.shoumun = 1;
		provinceCodes = $scope.translate.provinceCodes.map(function(item) {
			return item.provinceCode;
		});
		paramsData.provinceCodes = provinceCodes;
		$scope.startTime = $filter('date')($scope.translate.startTime, 'yyyyMMdd');
		$scope.endTime = $filter('date')($scope.translate.endTime, 'yyyyMMdd');
		paramsData.startTime = $filter('date')($scope.translate.startTime, 'yyyy-MM-dd');
		paramsData.endTime = $filter('date')($scope.translate.endTime, 'yyyy-MM-dd');
		if ($scope.shoumun === 0) {
			CoreLoading.setState(true);
			pbossOmServer.queryOrderRelation(paramsData, orderRelationData);
			exportExcel();
		}
	};
	function exportExcel(){
		$scope.outData = {
			startTime: $scope.startTime,
			endTime: $scope.endTime,
			title: $scope.startTime +'至'+$scope.endTime+'订购关系统计'
		};
		$scope.exportUrl = COREDATA.FORMPREFIX + 'OMdisplay/PBossOM/businessReportAnalysis/orderRelation/export'; //表导出的请求地址
	}

	$scope.clickForm = function(ev) {
		$mdDialog.show({
				controller: industryCtrl,
				templateUrl: 'views/oldOMDisplay/pbossOm/form/industry.html',
				targetEvent: ev
			})
			.then(searchIndustry);
	};
	function searchIndustry() {

	}

	$scope.resetValue = function() {
		$scope.translate = {
			businessCodes: [],
			startTime: new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate() - 31),
			endTime: new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate() - 1)
		};
	};

	
	function orderRelationData(res) {
		CoreLoading.setState(false);
		if (res.state === '10000' && res.data.pageData.length > 0) {
			$scope.flagObj = true;
			$scope.flag = true;
			$scope.orderData = res.data.pageData;

		}else{
			CoreService.dailog(res.state);
		}
	}


	$scope.queryOrderRelation();

}

function industryCtrl($scope, $mdDialog,productDataStores) {
	$scope.productD = [{
		productCode: '10001001',
		productName: '行车卫士(后装产品包)'
	}, {
		productCode: '10001002',
		productName: '行车卫士主体产品(集团)'
	}, {
		productCode: '10001003',
		productName: '行车卫士主集团增值包'
	}, {
		productCode: '10001004',
		productName: '行车卫士产品包(测试)'
	}, {
		productCode: '10001005',
		productName: '行车卫士主题产品(测试)'
	}, {
		productCode: '10001006',
		productName: '行车卫士专用APN,必选产品'
	}, {
		productCode: '10001007',
		productName: '行车卫士198元套餐(前装9折)'
	}];

	$scope.searchInfo ={
		productCode:'',
		productName:'',
		productNames:'全部'
	};
	$scope.flag = productDataStores.data.flag;
	$scope.toggle = function (item) {
	  var idx = productDataStores.data.productCode.indexOf(item.productCode);
	    if (idx > -1){
	    	productDataStores.data.productCode.splice(idx, 1);
	    	productDataStores.data.productName.splice(idx,1);
	    } else {
	    	productDataStores.data.productCode.push(item.productCode);
	    	productDataStores.data.productName.push(item.productName);
	    }
	   };

	$scope.exists = function (item) {
	  return productDataStores.data.productCode.indexOf(item.productCode) > -1;
	};

	$scope.getAll = function() {
		$scope.flag = !$scope.flag;

		if($scope.flag){
			productDataStores.data.productCode = $scope.productD.map(function(item){
				return item.productCode;
			});		 
			productDataStores.data.productName = [];
		}else{
			productDataStores.data.productCode = $scope.productD.map(function(item){
				return item.productCode;
			});		
			productDataStores.data.productName = $scope.productD.map(function(item){
				return item.productName;
			});
		}


		
	};
	$scope.checkFlag = function(){
		return 	productDataStores.data.productCode.length === $scope.productD.length ? true:false;
	};


	$scope.cancel = function() {
		
		$mdDialog.cancel();
	};
}

})();